package leetcode.code1387;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int getKth(int lo, int hi, int k) {
		int len = hi - lo + 1;
		int[][] arr = new int[len][2];
		for (int i = lo, p = 0; i <= hi; i++, p++) {
			arr[p][0] = this.k(i);
			arr[p][1] = i;
		}
		Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
		return arr[k - 1][1];
	}

	public int k(int num) {
		int ans = 0;
		while (num != 1) {
			if ((num & 1) == 0) {
				num >>= 1;
			} else {
				num = num * 3 + 1;
			}
			ans++;
		}
		return ans;
	}

	@Override
	public void debug4() {
		Map<Integer, List<Integer>> map = new HashMap<>();
		for (int i = 1; i < 1000; i++) {
			int k = this.k(i);
			map.computeIfAbsent(k, key -> new ArrayList<>()).add(i);
		}
		for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
			System.out.println("");
			System.out.print(entry.getKey() + " ");
			for (int num : entry.getValue()) {
				System.out.print(num + " ");
			}
		}

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
